﻿/*==============================================================
 * MsgService.cs
 * Copyright (C) 2014 贵州惠众互联 Inc. All rights reserved.
 *==============================================================
 * Author:   罗应红
 * Date:     2014/02/14 09:59:49.
 * Version:  1.0
 * QQ:		 88962800
*==============================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using QDT.Common;
using QDT.Common.Plugins;
using QDT.Core;
using QDT.Core.Domain;
using QDT.P2B.Domain.Messages;
using QDT.Services.DTO;
using MsgType = QDT.Core.Domain.Enums.MsgType;

namespace QDT.Services.SmMailModule {
    public class MsgService : ServiceBase,IMsgService {

        //private readonly ISmSendService _smSendService;

        public MsgService() {
        }

        //public MsgService(ISmSendService smSendService) {
        //    Check.Argument.IsNotNull(smSendService,"smSendService");
        //    _smSendService = smSendService;
        //}

        public IList<MsgTemplate> GetAllMsgTemplate() {
            return DbProvider.D<MsgTemplate>().ToList();
        }

        public MsgTemplate QueryMsgTemplateByIdentityName(string identityName) {
            return
                DbProvider.D<MsgTemplate>().FirstOrDefault(
                    o => o.Key.Equals(identityName, StringComparison.InvariantCultureIgnoreCase));
        }

        public MsgTemplate QueryMsgTemplateById(int id) {
            return DbProvider.GetByID<MsgTemplate>(id);
        }

        public void UpdateMsgTemplate(int id, string content, bool isHtml) {
            var msgTmp = DbProvider.GetByID<MsgTemplate>(id);
            if(msgTmp == null) {
                throw new Exception("消息模板不存在");
            }
            msgTmp.IsHtml = isHtml;
            msgTmp.Content = content;
            DbProvider.Update(msgTmp);
            DbProvider.SaveChanges();
        }

        //public void AddPromoteSm(SmDTO dto) {
        //    var entity = dto.ToEnttiy();
        //    DbProvider.Add(entity);
        //    DbProvider.SaveChanges();
        //    var smSendService = IoC.Resolve<ISmSendService>();
        //    smSendService.PromoteSmQueueSend();
        //}

        public void UpdatePromoteSm(SmDTO dto) {
            var entity = dto.ToEnttiy();
            var data =
                DbProvider.D<PromoteMsg>().FirstOrDefault(
                    o => o.InnerMsgType == (int) MsgType.ShortMsg && o.ID == entity.ID);
            if(data == null) {
                throw new Exception("你所编辑的短信不存在，请重新选择");
            }
            data.Content = entity.Content;
            data.SmsType = entity.SmsType;
            data.DealId = entity.DealId;
            data.Title = entity.Title;
            data.SendTime = entity.SendTime;
            data.SendType = entity.SendType;
            data.SendDefineData = entity.SendDefineData;
            data.SendTypeId = entity.SendTypeId;
            data.IsHtml = entity.IsHtml;
            DbProvider.Update(data);
            DbProvider.SaveChanges();
        }

        public void DeletePromoteSmForever(int[] ids) {
            if(!ids.Any()) {
                return;
            }
            foreach(var i in ids) {
                DbProvider.Delete<PromoteMsg>(o=>o.ID == i && o.InnerMsgType == (int) MsgType.ShortMsg);
            }
            DbProvider.SaveChanges();
        }

        public void DeletePromoteQueueForever(int[] ids) {
            if(!ids.Any()) {
                return;
            }
            foreach(var i in ids) {
                DbProvider.Delete<PromoteMsgList>(o=>o.ID == i);
            }
            DbProvider.SaveChanges();
        }

        public void ActiveSms(int id) {
            var data = DbProvider.GetByID<SmServer>(id);
            if(data == null) {
                return;
            }
            var allData = DbProvider.D<SmServer>().Where(o => o.ID != id);
            foreach(var  d in allData) {
                d.IsEffect = false;
                DbProvider.Update(d);
            }
            data.IsEffect = true;
            DbProvider.Update(data);
            DbProvider.SaveChanges();
        }

        //public SmsSendResult SendSignlePromoteMsg(int id) {
        //    var smSendService = IoC.Resolve<ISmSendService>();
        //   return  smSendService.PromoteSmSend(id);
        //}
    }
}